/**
*找回密码4步js验证
*/
define(function(require){
	var $=require("jq");
	
	/*********************验证第一步***************************/
	function SeekPassword1(area){
		var _this_=this;
		//保存验证区域
		this.seekBox=area;
		//保存用户名输入框
		this.userNameInput=$("#userName",area);
		this.userNameInput.keyup(function(){
			var $this=this;
			var value=this.value;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vUserName($this,value);
			},300);
		}).val("");
		//保存手机号输入框
		this.userPhoneInput=$("#userPhone",area);
		this.userPhoneInput.keyup(function(){
			var $this=this;
			var value=this.value;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vUserPhone($this,value);
			},300);
		}).focus(function(){
			//当获取焦点的时候，用户名是错误的，就返回设置用户名
			if(_this_.userNameInput.attr("isok")!="yes"){
				_this_.userNameInput.focus();
				_this_.goValidate(_this_.userNameInput,"输入用户名！",false);
				//_this_.goValidate(this,"用户名错误！",false);
			}else{
				$(this).next().hide();
			};
		}).val("");
		//获取验证码输入框
		this.validateCodeInput=$("#validateCode",area);
		this.validateCodeInput.keyup(function(){
			var $this=this;
			var value=this.value;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vCode($this,value);
			},300);
		}).val("");
		//获取换验证码对象
		this.changeCodeBtn=$("#changeCode",area);
		this.changeCodeBtn.click(function(){
			$("img",this).attr("src","/validateCode?"+Math.random());
		});
		//获取下一步按钮
		this.nextBtn=$(".next-btn",area);
		this.nextBtn.click(function(){
			_this_.submit();
		});
		$(document).keypress(function(e){
			if(e.which==13){
				_this_.submit();
			};
		});
		
	};
	SeekPassword1.prototype={
		submit:function(){
			if(this.userNameInput.attr("isok")!="yes"){
				this.userNameInput.keyup();
				return false;
			};
			if(this.userPhoneInput.attr("isok")!="yes"){
				this.userPhoneInput.keyup();
				return false;
			};
			if(this.validateCodeInput.attr("isok")!="yes"){
				this.validateCodeInput.keyup();
				return false;
			};	
			//进去下一步
			var userName=this.userNameInput.val();
			var phone=this.userPhoneInput.val();
			window.location="/findPwd2.do?userName="+userName+"&phone="+phone;
		},
		vCode:function(thisObj,value){
			//发送ajax验证
			if(value.isNumber()){
				this.ajaxCode(thisObj, value);
			}else{
				this.goValidate(thisObj,"验证码错误！",false);
			};
		},
		vUserPhone:function(thisObj,value){
			//发送ajax验证
			if(value.isMobile()){
				var userName=this.userNameInput.val()
				this.ajaxPhone(thisObj, value, userName);
			}else{
				this.goValidate(thisObj,"手机号错误！",false);
			};
		},
		vUserName:function(thisObj,value){
			if(value.isUserName()){
				this.ajaxUserName(thisObj, value);
			}else{
				this.goValidate(thisObj,"用户名错误！",false);
			};
		},
		goValidate:function(thisInput,text,isOk){
			var tipsBox=$(thisInput).parent().find(".validate-tips");
			var tipsText=$("strong",tipsBox);
			if(isOk){
				$(thisInput).attr("isok","yes");
				tipsBox.show().addClass("validate-tips-ok");
				tipsText.text(text);
			}else{
				$(thisInput).attr("isok","no");
				tipsBox.show().removeClass("validate-tips-ok");
				tipsText.text(text);
			};
		},
		ajaxUserName:function(thisObj,value){
			var _this_=this;
			$.post("/loginName.do",{userName:value},function(json){
				if(json.state=="success"){
					_this_.goValidate(thisObj,"用户名正确！",true);
				}else{
					_this_.goValidate(thisObj,"用户名不存在！",false);
				}
			},"json");
		},
		ajaxPhone:function(thisObj,phone,userName){
			var _this_=this;
			$.post("/loginPhone.do",{userName:userName,phone:phone},function(json){
				if(json.state=="success"){
					_this_.goValidate(thisObj,"手机号正确",true);
				}else{
					_this_.goValidate(thisObj,"手机号码与用户名不匹配！",false);
				}
			},"json");
		},
		ajaxCode:function(thisObj,value){
			var _this_=this;
			$.post("/loginCode.do",{validateCode:value},function(json){
				if(json.state=="success"){
					_this_.goValidate(thisObj,"验证码正确",true);
				}else{
					_this_.goValidate(thisObj,"验证码错误！",false);
				}
			},"json");
		}
	};
	//找回密码第一步
	if($("#J_SeekStep1").get(0)){
		var seekStep1=new SeekPassword1($("#J_SeekStep1"));
	};
	
	
	/*********************验证第二步***************************/
	
	function SeekPassword2(area){
		var _this_=this;
		//保存验证区域
		this.validateArea=area;
		//保存手机号码
		this.userPhone=$("#user-cellphone",area);
		//保存获取验证码按钮
		this.getCodeBtn=$("#get-validate-code",area);
		this.getCodeBtn.click(function(){
			
			if(!$(this).data("isGet")){
				//不让在点击，等一分钟后在点击
				$(this).data("isGet",true);
				_this_.codeInput.val("");
				_this_.codeInput.next().hide();
				//_this_.codeInput.val("").keyup();//wjygai
				_this_.ajaxSend(this);
			};
		});
		//获取验证码
		this.codeInput=$("#validateCode",area).val("");
		this.codeInput.keyup(function(){
			var value=this.value;
			var _this=this;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vCode(_this,value);
			},300);
		});
		//下一步
		this.nextBtn=$(".next-btn",area);
		this.nextBtn.click(function(){
			_this_.submit();
		});
		$(document).keypress(function(e){
			if(e.which==13){
				_this_.submit();
			};
		});
	};
	SeekPassword2.prototype={
		submit:function(){
			var _this_=this;
			if(this.codeInput.attr("isok")!="yes"){
				this.codeInput.keyup();
				return false;
			};
			var code=this.codeInput;
			$.post("/phoneCode.do",{phoneCode:this.codeInput.get(0).value},function(json){
				if(json.state=="success"){
					window.location="/findPwd3.do?phoneCode="+code.get(0).value;
				}else{
					code.attr("isok","no").next().show().removeClass("validate-tips-ok").find("strong").text("验证码错误,请重新获取");
					
					code.data("isGet",false);
					_this_.loop=0;
				}			
			},"json");
		},
		vCode:function(thisObj,value){
			if(value.isNumber()){
				$(thisObj).attr("isok","yes").next().show().addClass("validate-tips-ok").find("strong").text("验证码符合规范");
			}else{
				$(thisObj).attr("isok","no").next().show().removeClass("validate-tips-ok").find("strong").text("验证码应是数字");
			}
		},
		//设置一分钟后开起点击
		setOneMinuteAfter:function(thisObj){
			//发送验证码
			//$.post("",{},function(){});
			$(thisObj).next().show().addClass("validate-tips-ok").find("strong").text("发送成功");
			this.loop=60;
			_this_=this;
			$(thisObj).text("60秒后再获取");
			var timer=window.setInterval(function(){
				_this_.loop--;
				$(thisObj).text(_this_.loop+"秒后再获取");
				if(_this_.loop<0){
					//alert("ok");
					$(thisObj).text("重新获取").data("isGet",false);
					$(thisObj).next().hide();
					window.clearInterval(timer);
				};
			},1000);
		},
		ajaxSend:function(thisObj){
			var _this_=this;
			$.post("/sendPhone.do",{},function(json){
				if(json.state=="success"){
					_this_.setOneMinuteAfter(thisObj);
				}else if(json.state=="error"){
					var msg = "手机验证码发送失败 ,2分钟内请勿频繁刷验证码";
					alert(msg);
				}else{
					
				}
			},"json");
		}
	
	
	};
	
	if($("#J_SeekStep2").get(0)){
		var seekStep2=new SeekPassword2($("#J_SeekStep2"));
	};
	
	/*********************验证第三步***************************/
	function ResetPasswprd(main){
		var _this_=this;
		//保存验证区域
		this.main=main;
		//保存密码框
		this.passwordInput=$("#newPassword",main).val("");
		//保存提示信息和强度信息,强度级别
		this.tipsStrong=$(".tips-text b",main).eq(0);
		this.tipsText=$(".tips-text b",main).eq(1);
		this.strongLevel=$(".strong-level i",main);
		this.passwordInput.keyup(function(){
			var value=this.value;
			var $this=this;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vPassword($this,value);
			},300);
		});
		//保存再输入一次
		this.repeatPasswordInput=$("#repeatPassword");
		this.repeatPasswordInput.focus(function(){
			if(_this_.passwordInput.attr("isok")!="yes"){
				_this_.passwordInput.focus().keyup();
			};
		}).keyup(function(){
			var value=this.value;
			var $this=this;
			window.clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				_this_.vRepeatPassword($this,value);
			},300);
		});
		//获取提交按钮
		this.submitBtn=$(".next-btn",main);
		
		this.submitBtn.click(function(){
			_this_.submit();
		});
		$(document).keypress(function(e){
			if(e.which==13){
				_this_.submit();
			};
		});
	};
	ResetPasswprd.prototype={
		submit:function(){
			if(this.passwordInput.attr("isok")!="yes"){
				this.passwordInput.keyup();
				return;
			};
			if(this.repeatPasswordInput.attr("isok")!="yes"){
				this.repeatPasswordInput.keyup();
				return;
			};
			$("#subForm").submit();
		},
		//验证重复密码
		vRepeatPassword:function(thisObj,value){
			var newValue=this.passwordInput.val()
			if(value!=newValue){
				$(thisObj).attr("isok","no").next().show().removeClass("validate-tips-ok").find("strong").text("输入不一致");
			}else{
				$(thisObj).attr("isok","yes").next().show().addClass("validate-tips-ok").find("strong").text("输入正确");;
			};
		},
		//验证新密码
		vPassword:function(thisObj,value){
			if(value.isPassword()){
				$(thisObj).attr("isok","yes").next().show().addClass("validate-tips-ok");
				this.tipsText.text("密码输入正确");
				//如果正确就启动强度认证
				var level=this.evaluatePassword(value);
				this.strongLevel.removeClass("on").slice(0,level).addClass("on");
				if(level==1){
					this.tipsStrong.text("弱");
				}else if(level==2){
					this.tipsStrong.text("一般");
				}else if(level==3){
					this.tipsStrong.text("强");
				};
			}else{
				$(thisObj).attr("isok","no").next().show().removeClass("validate-tips-ok");
				this.tipsText.text("试试字母、数字、符号混搭");
				this.strongLevel.removeClass("on");
				this.tipsStrong.text("错误");
			};
			
		},
		//验证密码强度
		evaluatePassword:function(value){
			if (value == ""){  
				return 0;  
			}else if (value.length < 6)  {  
				return 1;  
			}else{  
				return value.match(/[a-z](?![^a-z]*[a-z])|[A-Z](?![^A-Z]*[A-Z])|\d(?![^\d]*\d)|[^a-zA-Z\d](?![a-zA-Z\d]*[^a-zA-Z\d])/g).length;  
			}; 
		}
	};
	
	if($("#J_SeekStep3").get(0)){
		var seekStep3=new ResetPasswprd($("#J_SeekStep3"));
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});